<?php

namespace app\common\model\operatingaccount;

use app\api\controller\Pub;
use app\common\model\user\User;
use app\common\model\company\Company;
use think\Model;


class Accountlink extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'operating_account_link';
    protected $pk    = 'Id';

    /**
     * 创建关联关系
     * @param  [type] $id         [出库台账id]
     * @param  [type] $goodweight [出库重量]
     * @param  [type] $goodid     [货物id]
     * @param  [type] $name       [货物名]
     * @return [type]             [description]
     */
    public static function create_out_link($id,$goodweight,$goodid,$goodname){
        //判断是否需要关联
        if(OperatingAccountLinkType=='user'){
            //用户手动添加关联，此时只需要添加一条待关联的数据就行。
            Accountlink::create(['AccountOutWarehouseId'=>$id]);   
        }else{
            $limitstar = 0;//查询数据的游标
            $idlist = [];//已经出完库 需要将endflag 设置为1的id列表
            $endflag = ['weight'=>0,'id'=>0];//最后一条记录的endflag
            //系统进行关联
            while ($goodweight>0) {
                $weightlist = Accountin::where('GoodId',$goodid)->where('GoodName',$goodname)->whereNull('EndFlag')->order('AccountTime asc')->field('Id,AccountTime,EndWeight,Quantity')->limit($limitstar,30)->select();
                $limitstar = $limitstar +30; //查询游标后移
                if(!empty($weightlist)){
                    //循环查询出来的结果 进行计算
                    foreach ($weightlist as $key => $value) {
                        if(!empty($value['EndWeight'])){
                            //如果还没有扣完重量则继续循环
                            $goodweight = $goodweight - $value['EndWeight'];
                        }else{
                            $goodweight = $goodweight - $value['Quantity'];
                        }
                        //已经减超了需要记录下 修改此id
                        if($goodweight<0){
                            $endflag['weight'] = 0-$goodweight;
                            $endflag['id'] = $value['Id'];
                        }else{
                            //如果正好减完或者还盈余，继续减。并将此id记录下来批量修改为 出完库的入库台账。
                            $idlist[] = $value['Id'];
                        }
                    }
                }else{
                    //没有找到对应的入库台账，创建一条等待关联的台账
                    Accountlink::create(['AccountOutWarehouseId'=>$id]);   
                }
                //判断是否有结束标志
                if(!empty($endflag['weight'])){
                    //将未结束的修改结束标志余量信息
                    Accountin::where('Id',$endflag['id'])->update(['EndWeight'=>$endflag['weight']]);
                    //添加关联表
                    Accountlink::create(['AccountOutWarehouseId'=>$id,'AccountInWarehouseId'=>$endflag['id']]);
                }
                //有需要关联的台账列表
                if(!empty($idlist)){
                    //修改已经出库完成的入库台账
                    Accountin::where('Id','in',$idlist)->update(['EndFlag'=>1]);
                    //增加入库台账与出库台账的关联
                    foreach ($idlist as $key => $value) {
                        $nlist[] = ['AccountOutWarehouseId'=>$id,'AccountInWarehouseId'=>$value];
                    }
                    $Accountlink = new Accountlink;
                    $Accountlink->saveAll($nlist);
                }
                if(count($weightlist)!==30){
                    if($goodweight>0){
                        //没有处理完重量，则创建一条等待关联的台账
                        Accountlink::create(['AccountOutWarehouseId'=>$id]);     
                    }
                    break;
                }
            }
        }
        return sok();
    }
}
